import { Injectable } from '@nestjs/common';
import { CreateAuthDto } from './dto/create-auth.dto';
import { UpdateAuthDto } from './dto/update-auth.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from '../user/entities/user.entity';
import { Repository } from 'typeorm';
import { UserService } from '../user/user.service';

@Injectable()
export class AuthService {
  constructor(
    @InjectRepository(User)
    private readonly userRep: Repository<User>,
    private readonly userService: UserService,
  ) { }
  async login(loginAuthDto: CreateAuthDto) {
    const { username, password } = loginAuthDto;
    const user = await this.userService.findOne(username);
    if (password === user.password) {
      return user;
    }
  }

  async register(registerAuthDto: CreateAuthDto) {
    const { username, password } = registerAuthDto;
    const user = await this.userRep.findOne({
      where: { username: username }
    });
    if (user) throw new Error('用户已存在');
    return this.userRep.save({
      username,
      password
    });
  }

  findAll() {
    return `This action returns all auth`;
  }

  findOne(id: number) {
    return `This action returns a #${id} auth`;
  }

  update(id: number, updateAuthDto: UpdateAuthDto) {
    return `This action updates a #${id} auth`;
  }

  remove(id: number) {
    return `This action removes a #${id} auth`;
  }
}
